package models

import (
	"gorm.io/gorm"
	"user-srv/basic/inits"

	"time"
)

// 收藏表
type Collect struct {
	Id        uint64         `gorm:"column:id;type:bigint UNSIGNED;comment:收藏id;primaryKey;not null;" json:"id"`                               // 收藏id
	UserId    int64          `gorm:"column:user_id;type:bigint;comment:用户id;not null;" json:"user_id"`                                         // 用户id
	GoodsId   int64          `gorm:"column:goods_id;type:bigint;comment:商品id;not null;" json:"goods_id"`                                       // 商品id
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);comment:创建时间;not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 创建时间
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);comment:修改时间;not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"` // 修改时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);comment:删除时间;default:NULL;" json:"deleted_at"`                          // 删除时间
}

func (c *Collect) TableName() string {
	return "collect"
}

// 根据id获取收藏
func (c *Collect) GetCollectById(collectId int) error {
	return inits.DB.Where("id = ?", collectId).Limit(1).Find(&c).Error
}

// 收藏添加
func (c *Collect) AddCollect() error {
	return inits.DB.Create(&c).Error
}

// 收藏展示
type CollectInfo struct {
	Id         int64   `gorm:"column:id"`          //收藏Id
	UserId     int64   `gorm:"column:user_id"`     //用户Id
	GoodsId    int64   `gorm:"column:goods_id"`    //商品Id
	GoodsName  string  `gorm:"column:goods_name"`  //商品名称
	GoodsPrice float64 `gorm:"column:goods_price"` //商品价格
	GoodsDesc  string  `gorm:"column:goods_desc"`  //商品简介
	GoodsUrl   string  `gorm:"column:goods_url"`   //商品图片
}

func (c *Collect) GetCollect() (collectList []*CollectInfo, err error) {
	err = inits.DB.Model(&Collect{}).
		Select("collect.*,goods.goods_name,goods.goods_price,goods.goods_desc,goods.goods_url").
		Joins("left join user on collect.user_id=user.id").
		Joins("left join goods on collect.goods_id=goods.id").
		Find(&collectList).Error
	return collectList, err
}

// 删除收藏
func (c *Collect) DeleteCollect(collectId int) error {
	return inits.DB.Where("id = ?", collectId).Delete(&c).Find(&c).Error
}
